package com.newx.auth.license.tool;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

public class CommonResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final int CODE_SUCCESS = 200;

    public static final int CODE_ERROR = 500;

    public static final String SHOW_TYPE_SILENT = "silent";

    public static final String SHOW_TYPE_SUCCESS = "success";

    public static final String SHOW_TYPE_WARNING = "warning";

    public static final String SHOW_TYPE_ERROR = "error";

    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "提示语")
    private String msg;

    @ApiModelProperty(value = "提示语类型(success、warning、error)")
    private String showType;
    @ApiModelProperty(value = "返回数据")
    private T data;

    public Integer getCode() {
        return this.code;
    }

    public CommonResult<T> setCode(Integer code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public CommonResult<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public CommonResult<T> setData(T data) {
        this.data = data;
        return this;
    }

    public String getShowType() {
        return this.showType;
    }

    public CommonResult<T> setShowType(String showType) {
        this.showType = showType;
        return this;
    }

    public CommonResult() {
    }

    /**
     * 成功
     * @return
     */
    public CommonResult(int code, String msg, String showType,T data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setShowType(showType);
        this.setData(data);
    }


    public static <T> CommonResult<T> success() {
        return new CommonResult<>(CODE_SUCCESS, "操作成功",SHOW_TYPE_SUCCESS, null);
    }
    //    public static <T> CommonResult<T> success(String msg) {
//        return new CommonResult<>(CODE_SUCCESS, msg, SHOW_TYPE_SUCCESS,null);
//    }
    public static <T> CommonResult<T> success(int code) {
        return new CommonResult<>(code, null, SHOW_TYPE_SUCCESS,null);
    }
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<>(CODE_SUCCESS, "操作成功", SHOW_TYPE_SUCCESS,data);
    }

    public static <T> CommonResult<T> warning() {
        return new CommonResult<>(CODE_ERROR, "操作成功",SHOW_TYPE_WARNING, null);
    }
    public static <T> CommonResult<T> warning(String msg) {
        return new CommonResult<>(CODE_ERROR, msg, SHOW_TYPE_WARNING,null);
    }
    public static <T> CommonResult<T> warning(int code) {
        return new CommonResult<>(code, null, SHOW_TYPE_WARNING,null);
    }
    public static <T> CommonResult<T> warning(T data) {
        return new CommonResult<>(CODE_ERROR, "操作成功", SHOW_TYPE_WARNING,data);
    }

    /**
     * 错误
     * @return
     */
    public static <T> CommonResult<T> error() {
        return new CommonResult<>(CODE_ERROR, "请求失败", SHOW_TYPE_ERROR,null);
    }
    public static <T> CommonResult<T> error(Integer code) {
        return new CommonResult<>(code, "请求失败", SHOW_TYPE_ERROR,null);
    }
    public static <T> CommonResult<T> error(String msg) {
        return new CommonResult<>(CODE_ERROR, msg, SHOW_TYPE_ERROR,null);
    }

    public static <T> CommonResult<T> error(T data) {
        return new CommonResult<>(CODE_ERROR, "请求失败", SHOW_TYPE_ERROR,data);
    }

    public static <T> CommonResult<T> error(Integer code,String msg) {
        return new CommonResult<>(code, msg, SHOW_TYPE_ERROR,null);
    }

    // 构建指定状态码
    public static <T> CommonResult<T> get(int code, String msg, String showType,T data) {
        return new CommonResult<>(code, msg, showType,data);
    }

    // 构建指定状态码
    public static <T> CommonResult<T> get(int code, String msg,T data) {
        return new CommonResult<>(code, msg, SHOW_TYPE_ERROR,data);
    }

    /*
     * toString()
     */
    @Override
    public String toString() {
        return "{"
                + "\"code\": " + this.getCode()
                + ", \"msg\": \"" + this.getMsg() + "\""
                + ", \"showType\": \"" + this.getShowType() + "\""
                + ", \"data\": \"" + this.getData() + "\""
                + "}";
    }
}
